/**
 * @Author: Bobby
 * @Date: 2022-03-09 14:40:46
 * @LastEditTime: 2022-03-22 17:34:38
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\ve\eventTask\actions\layer\openLayerId.js
 * @CIMC-V2
 */
import { findPrevParent } from '@ve/utils/utils'
import merge from 'lodash/merge'
import cloneDeep from 'lodash/cloneDeep'
export function openLayerId(vm, layerId, done, eventName, additionalData, splitPane) {
  console.log('表格打开图层', layerId)
  let getNowPageData = vm.$store.getters['preview/getNowPageData']
  //查找图层id的父级
  let parentObj = findPrevParent([getNowPageData], layerId, '_id', 'list')

  if (parentObj) {
    //查询到当前模块数据
    let layerObj = parentObj.list.find((v) => v._id === layerId)
    //图层数据
    let _layersData = {}
    let _requestData = {}
    if (vm.info && vm.info.type === 'uiTable') {
      let _tableRef = vm.vuiComponents[vm.info._id]
      if (_tableRef.self.hasCheckboxOrRadio == 'checkbox') {
        //获取表格选中数据
        // 这里判断一下获取附加值是否有值 可以不用getCheckboxRecords
        let _getCheckboxRecords = cloneDeep(_tableRef.ref.getCheckboxRecords())
        if (_getCheckboxRecords.length <= 0) {
          console.log('没有选择数据-1')
          // 修改任务队列执行状态，防止下一次事件被拦截
          console.log('vm -----------tttttttttttttttttttttttt------------ ', vm, eventName)
          vm[`${eventName}IsRunning`] = false
          return false
        }
        // _layersData[vm.info.dataName] = _getCheckboxRecords
        //默认选中条目打开数据当requestData
        const oldRequestData = cloneDeep(layerObj.data.requestData)
        _requestData = cloneDeep(merge(oldRequestData, _getCheckboxRecords[0]))
      } else {
        //获取表格选中数据
        let _getRadioRecords = _tableRef.ref.getRadioRecord()
        if (_getRadioRecords) {
          console.log('没有选择数据-2')
          // 修改任务队列执行状态，防止下一次事件被拦截
          vm[`${eventName}IsRunning`] = false
          return false
        }
        // _layersData[vm.info.dataName] = _getRadioRecords
        //默认选中条目打开数据当requestData
        _requestData = cloneDeep(merge(layerObj.data.requestData, _getRadioRecords))
      }
    }
    if (vm.info && (vm.info.type === 'uiDynamicTable' || vm.info.type === 'uiFlowChart')) {
      // 如果点击的是动态表格组件或流程图组件
      _requestData = cloneDeep(merge(layerObj.data.requestData, additionalData.data))
    }
    // 取消合并页面数据（这里暂时不考虑先数据注入再打开页面的情况）
    // else {
    //   _requestData = cloneDeep(layerObj.data.requestData)
    // }

    // 提交 dispatch 触发页面组件状态更新（条件判断 -- nzl; dataObj 为传递的数据对象，moduleObj 为目标组件对象
    // vm.$store.dispatch('pageModulesState/setPageModulesStateByObj', { dataObj: _requestData, moduleObj: layerObj })
    if (!(process.env.VUE_APP_BuildProjectType === 'mobile' || vm.$store.state.diy.settings.mode == 'mobile')) {
      vm.$store.dispatch('pageModulesState/setPageModulesStatesByObj', {
        pageId: vm.$store.getters['preview/getNowPageId'],
        pageState: { dataObj: _requestData, moduleObj: layerObj },
      })
    }

    // console.log('进 ---------- openLayerId', layerObj, parentObj)
    //当前图层父级
    layerObj._parentId = parentObj._id
    //可以通过showSubPageId来定位修改值
    parentObj.showSubPageId = layerObj._id

    //subPage.dialogHeight 如果被重新赋值 会影响子弹框的宽高
    parentObj.subPage._dialogHeight = layerObj.subPage.dialogHeight
    parentObj.subPage._dialogWidth = layerObj.subPage.dialogWidth
    parentObj.subPage.boxInnerHeight = layerObj.subPage.boxInnerHeight || 1920
    parentObj.subPage.boxInnerWidth = layerObj.subPage.boxInnerWidth || 1080

    parentObj.subPage.clickToClose = layerObj.subPage.clickToClose || false

    parentObj.subPage.type = layerObj.type
    if (layerObj.type == 4 && additionalData) {
      parentObj.subPage.popX = Math.floor(additionalData.x)
      parentObj.subPage.popY = Math.floor(additionalData.y)
    }
    if (layerObj.type == 1) {
      parentObj.subPage.info = layerObj
    } else {
      parentObj.subPage.dialog = layerObj
    }

    parentObj.hasSubPageShow = true

    // 如果有选择子图层的打开位置 则需要在指定位置打开
    if (splitPane.splitId && splitPane.paneId) {
      // 记录分割组件信息
      layerObj.isSplitLayer = true
      layerObj.splitModuleInfo = splitPane
      const splitModule = vm.vuiComponents[splitPane.splitId] && vm.vuiComponents[splitPane.splitId].self
      if (splitModule) {
        splitModule.info.showCustomPage = true
        splitModule.info.customPageInfo = {
          pageInfo: layerObj,
          pagePosition: splitPane.paneId,
          parentPageId: parentObj._id,
        }
      }
    }

    setTimeout(() => {
      layerObj.data = {
        ...layerObj.data,
        ...{
          //所有图层选择后的数据
          layersData: _layersData,
        },
        ...{
          requestData: _requestData,
        },
      }
      // 将下一次执行队列放入定时器 保证先触发 data 改变的动作
      setTimeout(() => {
        console.log('打开页面执行完成')
        done(null, {})
      }, 0)

      console.log('测试页面 打开图层通知', `SplitpaneResized_${vm.previewPageId()}`)
      vm.$pub(`SplitpaneResized_${vm.previewPageId()}`, true)
    }, 1)
  } else {
    done(null, {})
  }
}
